package com.agile.leetcode.jvm.outofheapmemory;


import org.caffinitas.ohc.OHCache;
import org.caffinitas.ohc.OHCacheBuilder;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author:ChenZhangKun
 * @Date: 2023/2/6 21:10
 */
public class CompareDemo {
    private static Map<String, String> map = new HashMap();
    private static OHCache ohCache = OHCacheBuilder.<String, String>newBuilder()
            // key的序列化方式
            .keySerializer(Demo.cacheSerializer)
            // value的序列化方式
            .valueSerializer(Demo.cacheSerializer)
            // 定义2G
            .capacity(1027 * 1024 * 2)

            .build();

    public static void main(String[] args) {
        /**
         * 会内存溢出
         */
        new Thread(() -> {
            while (true) {
                map.put("czk", "czk");
            }
        }).start();
        /**
         * 不会内存溢出
         */
        new Thread(() -> {
            while (true) {
                ohCache.put("czk", "czk");
            }

        }).start();
    }
}
